﻿using ArcGIS.Core.Data.UtilityNetwork.Trace;
using ArcGIS.Core.Data;
using ArcGIS.Core.Geometry;
using ArcGIS.Desktop.Catalog;
using ArcGIS.Desktop.Core;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using CCTool.Scripts.Manager;
using CCTool.Scripts.ToolManagers;
using CCTool.Scripts.ToolManagers.Extensions;
using CCTool.Scripts.ToolManagers.Library;
using CCTool.Scripts.ToolManagers.Managers;
using NPOI.OpenXmlFormats.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CCTool.Scripts.GDBMenu
{
    /// <summary>
    /// Interaction logic for ChangeXYTolerance.xaml
    /// </summary>
    public partial class ChangeXYTolerance : ArcGIS.Desktop.Framework.Controls.ProWindow
    {
        // 工具设置标签
        readonly string toolSet = "ChangeXYTolerance";
        public ChangeXYTolerance()
        {
            InitializeComponent();


            // 初始化参数选项
            textBox_xyTol.Text = BaseTool.ReadValueFromReg(toolSet, "xyTol", "0.001");
            textBox_xyRes.Text = BaseTool.ReadValueFromReg(toolSet, "xyRes", "0.0001");

        }

        // 定义一个进度框
        private ProcessWindow processwindow = null;
        string tool_name = "整库更改XY容差";

        private async void btn_go_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // 参数获取
                double xyTol = textBox_xyTol.Text.ToDouble();
                double xyRes = textBox_xyRes.Text.ToDouble();

                // 默认数据库位置
                var defGDB = Project.Current.DefaultGeodatabasePath;


                // 判断参数是否选择完全
                if (xyTol >= 1 || xyRes >= 1 || xyTol <= 0 || xyRes <= 0)
                {
                    MessageBox.Show("参数有误！");
                    return;
                }
                // 写入本地
                BaseTool.WriteValueToReg(toolSet, "xyTol", xyTol);
                BaseTool.WriteValueToReg(toolSet, "xyRes", xyRes);

                // 打开进度框
                ProcessWindow pw = UITool.OpenProcessWindow(processwindow, tool_name);
                pw.AddMessageTitle(tool_name);

                Close();

                await QueuedTask.Run(() =>
                {
                    pw.AddMessageStart("获取要素类");
                    // 获取当前选择的gdb数据库
                    string gdbPath = Project.Current.SelectedItems.OfType<GDBProjectItem>().FirstOrDefault().Path;

                    // 要素类
                    List<string> originFCList = gdbPath.GetOnlyFeatureClassPathFromGDB();
                    // 中间数据
                    string temFc = $@"{defGDB}\temFc";

                    pw.AddMessageMiddle(10, "【单独的要素类】", Brushes.Blue);
                    foreach (string fc in originFCList)
                    {
                        string name = fc.TargetFcFullName();
                        
                        pw.AddMessageMiddle(40 / originFCList.Count, name, Brushes.Gray);
                        // 复制要素
                        Arcpy.CopyFeatures(fc, temFc, xyTolerance: xyTol.ToString(), xyResolution: xyRes.ToString());
                        Arcpy.CopyFeatures(temFc, fc);
                    }

                    pw.AddMessageMiddle(0, "【要素数据集】", Brushes.Blue);
                    // 要素数据集
                    List<string> dbList = gdbPath.GetDataBasePath();
                    foreach (string db in dbList)
                    {
                        //  要素数据集的名称
                        string dbName = db[(db.LastIndexOf(".gdb") + 5)..];
                        pw.AddMessageMiddle(40 / dbList.Count, dbName);

                        SpatialReference sr = db.TargetSpatialReferenceFromDB();
                        // 如果存在数据集，就先删除
                        if (defGDB.IsHaveDataset(dbName))
                        {
                            Arcpy.Delect($@"{defGDB}\{dbName}");
                        }
                        // 创建数据集
                        Arcpy.CreateFeatureDataset(defGDB, dbName, sr, xyTol, xyRes);

                        // 查找数据集下的要素类
                        List<string> allFCList = gdbPath.GetFeatureClassPathFromGDB();
                        foreach (string fc in allFCList)
                        { 
                            if (fc.Contains( db))
                            {
                                string name = fc.TargetFcName();
                                pw.AddMessageMiddle(0, name, Brushes.Gray);
                                // 复制要素
                                Arcpy.CopyFeatures(fc, $@"{defGDB}\{dbName}\{name}");
                            }
                        }

                        // 删除原始数据集，并复制新数据集
                        Arcpy.Delect(db);
                        Arcpy.Copy($@"{defGDB}\{dbName}", db);
                        Arcpy.Delect($@"{defGDB}\{dbName}");
                    }


                });
                pw.AddMessageEnd();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message + ee.StackTrace);
                return;
            }
        }

        private void btn_help_click(object sender, RoutedEventArgs e)
        {
            string url = "https://blog.csdn.net/xcc34452366/article/details/152792906";
            UITool.Link2Web(url);
        }
    }
}
